package com.mall507.fivepeaks.widget.xdialog;

import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.Util;

/**
 * 用户邀请码定制code
 * */
public class XUserYqCodeDialog{

	private Dialog alertDialog;
	
	
	/**
	 * @param context
	 * @param code 邀请码
	 * @param callback 选择结果回调
	 * */
	public void show(final Context context,final String code,final XUserYqCodeDialogCallBack callback) {
		//防止网络延迟情况下多次点击弹出多个窗口
		dismiss();
		alertDialog = new Dialog(context, R.style.XDialog_RemoveAllStyle);
		LayoutInflater in = LayoutInflater.from(context);
		View viewDialog = in.inflate(R.layout.view_xdialog_user_yq_code, null);
		TextView codeText=(TextView) viewDialog.findViewById(R.id.code);
		codeText.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View view) {
				ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
				ClipData myClip=ClipData.newPlainText("UserYqCode", code);
				cmb.setPrimaryClip(myClip);
				Toast.makeText(context, "成功复制:"+code, Toast.LENGTH_SHORT).show();
				return true;
			}
		});
		for(int i=0;i<code.length();i++){
			codeText.setText(codeText.getText()+code.substring(i, i+1)+"\u0020");
		}
		if(Util.isEmpty(codeText.getText().toString())){
			codeText.setText("\u0020\u0020暂无邀请码\u0020\u0020");
		}else{
			codeText.setText("\u0020\u0020"+codeText.getText()+"\u0020");
		}
		TextView button=(TextView) viewDialog.findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				callback.confirm();
			}
		});
		alertDialog.setContentView(viewDialog);
		// 点击对话框外部取消对话框显示
		alertDialog.setCanceledOnTouchOutside(true);
		alertDialog.show();
	}
	
	/**
	 * 关闭当前Dialog
	 * */
	public void dismiss(){
		if(alertDialog!=null && alertDialog.isShowing()){
			alertDialog.dismiss();
		}
	}
	
	/**
	 * 自编邀请码
	 * */
	public interface XUserYqCodeDialogCallBack{
		  void confirm();
		  
	}
}
